www.gusucode.com > VC++ 桌面透明数字时钟源码-源码程序 > VC++ 桌面透明数字时钟源码-源码程序/code/FileDlg.cpp
// FileDlg.cpp : implementation file // Download by http://www.NewXing.com #include "stdafx.h" #include "File.h" #include "FileDlg.h" #include "SetDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() NOTIFYICONDATA tray; ///////////////////////////////////////////////////////////////////////////// // CFileDlg dialog int CFileDlg::a=0; int CFileDlg::b=0; int CFileDlg::m_fx=0; int CFileDlg::m_fy=0; CFileDlg::CFileDlg(CWnd* pParent /*=NULL*/) : CDialog(CFileDlg::IDD, pParent) { //{{AFX_DATA_INIT(CFileDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } CFileDlg::~CFileDlg() { Shell_NotifyIcon(NIM_DELETE,&tray); } void CFileDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CFileDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CFileDlg, CDialog) //{{AFX_MSG_MAP(CFileDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_CTLCOLOR() ON_WM_TIMER() ON_UPDATE_COMMAND_UI(IDR_FRONT, OnUpdateFront) ON_WM_INITMENUPOPUP() ON_UPDATE_COMMAND_UI(IDR_LAYERED, OnUpdateLayered) //}}AFX_MSG_MAP ON_MESSAGE(WM_MYMESSAGE , OnMessage) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFileDlg message handlers BOOL CFileDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); tray.cbSize=sizeof(NOTIFYICONDATA); tray.hWnd=this->m_hWnd; tray.uID=IDR_MAINFRAME; tray.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME)); strcpy(tray.szTip,"桌面时钟1.0\n按一秒退出\n双击设置参数)"); tray.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP; tray.uCallbackMessage=WM_MYMESSAGE; Shell_NotifyIcon(NIM_ADD,&tray); ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW);//实现程序后台功能(即不再任务栏显示不再任务管理器中显示) SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE, GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)|0x80000); HINSTANCE hInst = LoadLibrary("User32.DLL"); if(hInst) { typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD); MYFUNC fun = NULL; //取得SetLayeredWindowAttributes函数指针 fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes"); if(fun) { fun(this->GetSafeHwnd(),RGB(183,94,28),220,1); } FreeLibrary(hInst); } CString str77; char m_pl[10]; memset(m_pl,0,10*sizeof(char)); ::GetPrivateProfileString("SYSTEM","m_booll","",m_pl,10,".\\setup.ini"); str77.Format("%s",m_pl); if(str77=="true") { ModifyStyleEx(0,WS_EX_TRANSPARENT); } if(str77=="false") { ModifyStyleEx(WS_EX_TRANSPARENT,0); } SetTimer(1,1000,NULL); m_fx=::GetPrivateProfileInt("SYSTEM","m_px",0,".\\setup.ini"); m_fy=::GetPrivateProfileInt("SYSTEM","m_py",0,".\\setup.ini"); int m_x,m_y; m_x=::GetPrivateProfileInt("SYSTEM","m_px",0,".\\setup.ini"); m_y=::GetPrivateProfileInt("SYSTEM","m_py",0,".\\setup.ini"); CString strf; char m_pf[10]; memset(m_pf,0,10*sizeof(char)); ::GetPrivateProfileString("SYSTEM","m_boolf","",m_pf,10,".\\setup.ini"); strf.Format("%s",m_pf); if(strf=="true") { ::SetWindowPos(this->GetSafeHwnd(), CWnd::wndTopMost,m_fx,m_fy, 0, 0, SWP_NOSIZE); } if(strf=="false") { ::SetWindowPos(this->GetSafeHwnd(), CWnd::wndNoTopMost,m_fx,m_fy, 0, 0, SWP_NOSIZE); } SYSTEMTIME st; CString str101; GetLocalTime(&st); CStatic *pWnd=(CStatic*)GetDlgItem(IDC_STATIC); if(pWnd) { if(st.wMinute<=9) { if(st.wSecond<=9) { str101.Format("%2d:%d%d:%d%d",st.wHour,0,st.wMinute,0,st.wSecond); } else { str101.Format("%2d:%d%d:%d",st.wHour,0,st.wMinute,st.wSecond); } } else if(st.wSecond<=9) { if(st.wMinute<=9) { str101.Format("%2d:%d%d:%d%d",st.wHour,0,st.wMinute,0,st.wSecond); } else { str101.Format("%2d:%d:%d%d",st.wHour,st.wMinute,0,st.wSecond); } } else { str101.Format("%2d:%d:%d",st.wHour,st.wMinute,st.wSecond); } int m_zt,m_pound; m_pound=::GetPrivateProfileInt("SYSTEM","m_pound",0,".\\setup.ini"); m_zt=::GetPrivateProfileInt("SYSTEM","m_font",0,".\\setup.ini"); // CWnd *pWnd=GetDlgItem(IDC_STATIC); CFont *pFont=new CFont; pFont->CreateFont(m_zt,0,0,0,m_pound,FALSE,FALSE,0,GB2312_CHARSET,OUT_STRING_PRECIS,CLIP_MASK,PROOF_QUALITY,DEFAULT_PITCH|FF_SWISS,"宋体"); pWnd->SetFont(pFont); pWnd->SetWindowText(str101); } // TODO: Add extra initialization here // m_EventEdit.SetFocus(); return FALSE; // return TRUE unless you set the focus to a control } void CFileDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CFileDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CFileDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } HBRUSH CFileDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { if(nCtlColor==CTLCOLOR_DLG) { // pDC->SetTextColor(RGB(0,0,255)); //pDC->SetBkColor(RGB(25,45,86)); HBRUSH B=CreateSolidBrush(RGB(183,94,28)); return B; } if(nCtlColor==CTLCOLOR_EDIT||CTLCOLOR_STATIC) { int m_red,m_grn,m_blu; m_red=::GetPrivateProfileInt("SYSTEM","m_r",0,".\\setup.ini"); m_grn=::GetPrivateProfileInt("SYSTEM","m_g",0,".\\setup.ini"); m_blu=::GetPrivateProfileInt("SYSTEM","m_b",0,".\\setup.ini"); pDC->SetTextColor(RGB(m_red,m_grn,m_blu));//编辑框静态文本框字体颜色 pDC->SetBkColor(RGB(183,94,28));//用来设置文本当前状态(字体占据的部分)时的背景色 HBRUSH A=CreateSolidBrush(RGB(183,94,28));//用来设置编辑框及静态文本(字体没有占据的部分)的背景色 return A; } } void CFileDlg::OnCancel() { // TODO: Add extra cleanup here CDialog::OnCancel(); } void CFileDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent==1) { SYSTEMTIME st; CString str; GetLocalTime(&st); CStatic *pWnd=(CStatic*)GetDlgItem(IDC_STATIC); if(pWnd) { if(st.wMinute<=9) { if(st.wSecond<=9) { str.Format("%2d:%d%d:%d%d",st.wHour,0,st.wMinute,0,st.wSecond); } else { str.Format("%2d:%d%d:%d",st.wHour,0,st.wMinute,st.wSecond); } } else if(st.wSecond<=9) { if(st.wMinute<=9) { str.Format("%2d:%d%d:%d%d",st.wHour,0,st.wMinute,0,st.wSecond); } else { str.Format("%2d:%d:%d%d",st.wHour,st.wMinute,0,st.wSecond); } } else { str.Format("%2d:%d:%d",st.wHour,st.wMinute,st.wSecond); } pWnd->SetWindowText(str); } } if(nIDEvent==2) { //srand((unsigned)time(NULL)); b=rand()%950; a=rand()%1050; CString str; char m_p[10]; memset(m_p,0,10*sizeof(char)); ::GetPrivateProfileString("SYSTEM","m_boolf","",m_p,10,".\\setup.ini"); str.Format("%s",m_p); if(str=="true") { ::SetWindowPos(this->GetSafeHwnd(), CWnd::wndTopMost,a,b, 0, 0, SWP_NOSIZE); } if(str=="false") { ::SetWindowPos(this->GetSafeHwnd(), CWnd::wndNoTopMost,a,b, 0, 0, SWP_NOSIZE); } } if(nIDEvent==3) { ::SendMessage(m_hWnd,WM_CLOSE,0,0); } CDialog::OnTimer(nIDEvent); } LRESULT CFileDlg::OnMessage(WPARAM wparam,LPARAM lparam) { switch(lparam) { case WM_RBUTTONUP: { POINT point; GetCursorPos(&point); HMENU hMenu,hSubMenu; hMenu=LoadMenu(NULL,MAKEINTRESOURCE(IDR_MENU1)); hSubMenu=GetSubMenu(hMenu,0); ::SetForegroundWindow(this->m_hWnd); int n=TrackPopupMenu(hSubMenu,TPM_RETURNCMD|TPM_LEFTBUTTON,point.x,point.y,0,m_hWnd,NULL); switch(n) { case IDR_EXIT: { KillTimer(1); KillTimer(2); ::SendMessage(m_hWnd,WM_CLOSE,0,0); break; } case IDR_SKIP: { int m_t; m_t=::GetPrivateProfileInt("SYSTEM","m_time",0,".\\setup.ini"); SetTimer(2,m_t*1000,NULL); srand((unsigned)time(NULL)); break; } case IDR_PRIME: { KillTimer(2); CString str; char m_p[10]; memset(m_p,0,10*sizeof(char)); ::GetPrivateProfileString("SYSTEM","m_boolf","",m_p,10,".\\setup.ini"); str.Format("%s",m_p); if(str=="true") { ::SetWindowPos(this->GetSafeHwnd(), CWnd::wndTopMost,m_fx,m_fy, 0, 0, SWP_NOSIZE); } if(str=="false") { ::SetWindowPos(this->GetSafeHwnd(), CWnd::wndNoTopMost,m_fx,m_fy, 0, 0, SWP_NOSIZE); } break; } case IDR_STOP: { KillTimer(2); break; } case IDR_SET: { CSetDlg *pdlg; pdlg=new CSetDlg; pdlg->Create(IDD_SET,NULL); pdlg->ShowWindow(SW_SHOW); break; } case IDR_FRONT: { CString str; char m_p[10]; memset(m_p,0,10*sizeof(char)); ::GetPrivateProfileString("SYSTEM","m_boolf","",m_p,10,".\\setup.ini"); str.Format("%s",m_p); if(str=="true") { ::SetWindowPos(this->GetSafeHwnd(), CWnd::wndNoTopMost,0,0, 0, 0, SWP_NOSIZE|SWP_NOMOVE); ::WritePrivateProfileString("SYSTEM","m_boolf","false",".\\setup.ini"); } if(str=="false") { ::SetWindowPos(this->GetSafeHwnd(), CWnd::wndTopMost,0,0, 0, 0, SWP_NOSIZE|SWP_NOMOVE); ::WritePrivateProfileString("SYSTEM","m_boolf","true",".\\setup.ini"); } break; } case IDR_LAYERED: { CString str; char m_p[10]; memset(m_p,0,10*sizeof(char)); ::GetPrivateProfileString("SYSTEM","m_booll","",m_p,10,".\\setup.ini"); str.Format("%s",m_p); if(str=="true") { ModifyStyleEx(WS_EX_TRANSPARENT,0); // ModifyStyleEx(0,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000); ::WritePrivateProfileString("SYSTEM","m_booll","false",".\\setup.ini"); } if(str=="false") { ModifyStyleEx(0,WS_EX_TRANSPARENT); ::WritePrivateProfileString("SYSTEM","m_booll","true",".\\setup.ini"); } } break; } break; } case WM_LBUTTONDOWN: { SetTimer(3,1000,NULL); break; } case WM_LBUTTONUP: { KillTimer(3); break; } case WM_LBUTTONDBLCLK: { CSetDlg *pdlg; pdlg=new CSetDlg; pdlg->Create(IDD_SET,NULL); pdlg->ShowWindow(SW_SHOW); break; } case WM_MBUTTONUP: { int m_px,m_py,m_fzt,m_pound; m_pound=::GetPrivateProfileInt("SYSTEM","m_pound",0,".\\setup.ini"); m_fzt=::GetPrivateProfileInt("SYSTEM","m_font",0,".\\setup.ini"); m_px=::GetPrivateProfileInt("SYSTEM","m_px",0,".\\setup.ini"); m_py=::GetPrivateProfileInt("SYSTEM","m_py",0,".\\setup.ini"); ::SetWindowPos(m_hWnd,CWnd::wndTopMost, m_px,m_py, 0, 0, SWP_NOSIZE); CWnd *pWnd=GetDlgItem(IDC_STATIC); CFont *pFont=new CFont; pFont->CreateFont(m_fzt,0,0,0,m_pound,FALSE,FALSE,0,0,0,0,0,0,"宋体"); pWnd->SetFont(pFont,true); break; } break; } return 0; } void CFileDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) { CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu); if(!bSysMenu && pPopupMenu) { CCmdUI cmdUI; cmdUI.m_pOther=NULL; cmdUI.m_pMenu=pPopupMenu; cmdUI.m_pSubMenu=NULL; UINT count=pPopupMenu->GetMenuItemCount(); cmdUI.m_nIndexMax=count; for(UINT i=0; i<count; i++) { UINT nID=pPopupMenu->GetMenuItemID(i); if(-1 == nID || 0 == nID) { continue; } cmdUI.m_nID = nID; cmdUI.m_nIndex = i; cmdUI.DoUpdate(this,FALSE); } } } void CFileDlg::OnUpdateFront(CCmdUI* pCmdUI) { CString str9; char m_p1[10]; memset(m_p1,0,10*sizeof(char)); ::GetPrivateProfileString("SYSTEM","m_boolf","",m_p1,10,".\\setup.ini"); str9.Format("%s",m_p1); if(str9=="true") { pCmdUI->SetCheck(1); } if(str9=="false") { pCmdUI->SetCheck(0); } } void CFileDlg::OnUpdateLayered(CCmdUI* pCmdUI) { CString str4; char m_p2[10]; memset(m_p2,0,10*sizeof(char)); ::GetPrivateProfileString("SYSTEM","m_booll","",m_p2,10,".\\setup.ini"); str4.Format("%s",m_p2); if(str4=="true") { pCmdUI->SetCheck(1); } if(str4=="false") { pCmdUI->SetCheck(0); } }